package mytest.wd2;

import com.google.gson.Gson;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import pojo.vo.NameListVo;
import utils.AESUtils;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class ExcelToJson {
    private final static String aesKey = "2:0dcd9c9b5c5048d7972c5ee298a49c15";
    private final static String sheetName = "B2-8";
    private final static String ds = "2024-08-01";
    private final static String marketType = "B1";

    public static void main(String[] args) throws Exception {

        //本地要读取的excel
        String excelFilePath = "E:\\QQandWeChat downloads\\WXWork\\1688857665631195\\Cache\\File\\2025-02\\B2-8-2.xlsx";

        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String t0TaskDate = currentDate.format(formatter);

        String batchId = "8c11fcf7-f44a-4b90-b644-18c9817c547a";

        //json文件保存的文件路径
        String fileDate = t0TaskDate.replace("-", "");
        String filePath = "E:\\AfterSchool\\标准化\\json_4\\json_" + ds + "_" + sheetName + "_2.txt";

        BufferedWriter writer = new BufferedWriter(new FileWriter(filePath));
        FileInputStream fileInputStream = new FileInputStream(excelFilePath);
        XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
        int numberOfSheets = workbook.getNumberOfSheets();

        System.out.println(marketType + ":" + batchId);

        int lastCellNum = (int)workbook.getSheetAt(0).getRow(0).getLastCellNum();

        for (int i = 0; i < numberOfSheets; i++) {
            Sheet sheet = workbook.getSheetAt(i);
            switch (sheet.getSheetName()) {
                case sheetName:
                    for (int j = 1; j <= sheet.getLastRowNum(); j++) {
                        Row row = sheet.getRow(j);
                        NameListVo standard = new NameListVo();
                        standard.setDs(ds);
                        standard.setBatch_id(batchId);
                        standard.setPhone(AESUtils.encrypt(String.valueOf(row.getCell(0)), aesKey));
                        standard.setMarket_type(marketType);
                        Map<String, String> jsonRow = new HashMap<>();
                        for (int i1 = 1; i1 < lastCellNum; i1++) {
                            jsonRow.put(String.valueOf(sheet.getRow(0).getCell(i1)), String.valueOf(row.getCell(i1)));
                        }
                        standard.setJson_raw(jsonRow);
                        writer.write(new Gson().toJson(standard) + "\r\n");
                    }
                    writer.close();
                    break;
            }
        }

        System.out.println("success");

    }
}
